package com.cx.update;


import android.annotation.SuppressLint;

import org.json.JSONArray;
import org.json.JSONObject;

@SuppressLint({"DefaultLocale"})
public class VersionParser {
    public VersionParser() {
    }

    public static int updateUrl(String url, String oldVersion, UpdateUtils updateUtils,String rsaMd5) {
        String str = HttpUtil.getString(url, 15000);
       System.out.println(url+" == "+str);
        return !isEmptyText(str)?parserJson(str, oldVersion, updateUtils,rsaMd5):-1;
    }
    /**
     *版本比较
     * @param curVersion
     * @param newVersion
     * @return  newVersion > curVersion
     */
    private static boolean compareVersion(String curVersion, String newVersion)
    {
        try {
            curVersion = curVersion.replace("V", "");
            curVersion = curVersion.replace("v", "");
            newVersion = newVersion.replace("V", "");
            newVersion = newVersion.replace("v", "");
            String[] curArr = curVersion.length()>0?curVersion.split("\\."):new String[0];
            String[] newArr = newVersion.length()>0?newVersion.split("\\."):new String[0];

            int len = newArr.length;
            int clen = curArr.length;
//            System.out.println(clen+" clen "+curVersion+" + "+curArr);
            int newV, curV;
            for (int i = 0; i < len; i++) {
                newV = Integer.parseInt(newArr[i], 10);
                if (i < clen) {
                    curV = Integer.parseInt(curArr[i], 10);
                    if (newV > curV)
                        return true;
                    else if (newV < curV)
                        return false;
                } else {
                    if (newV > 0) return true;
                }
            }
        }catch(Exception ex){System.out.println(ex.getMessage());}
        return false;
    }
    @SuppressLint({"DefaultLocale"})
    private static int parserJson(String info, String oldVersion, UpdateUtils updateUtils, String rsaMd5) {
        UpdateInfo updateInfo = new UpdateInfo();;
        int ret = -3;
        try {
            JSONObject e = new JSONObject(info);
            JSONArray packs = e.getJSONArray("pack");
            int x = 0;
            String testIP="";
            String version = oldVersion;
            String  maxversion="";
            int size = packs.length();
            System.out.println(rsaMd5+" rsaMd5");
            final String TestRSA = "ae8de2f271a6d0c5d2cae3d30f55de86";
            for (; x < size; ++x) {
                JSONObject packInfo = packs.getJSONObject(x);
                String _rsaMd5 = packInfo.getString("rsaMd5");

                if (rsaMd5.equals(_rsaMd5)||TestRSA.equals(_rsaMd5)) {

                    String maskVersion = packInfo.getString("version");
                    try{
                        testIP = packInfo.getString("TestIP");
                    }
                    catch (Exception ex)
                    {
                        testIP="";
                    }
                    if(!isEmptyText(testIP))
                    {
                        String localityip= MacUtil.getMac();
                        if(!testIP.equals(localityip))
                            continue;
                    }
//                    System.out.println(maxversion+" maxversion "+maskVersion);
                    if(compareVersion(maxversion,maskVersion))
                    {
                        maxversion =maskVersion ;
                        UpdateInfo mupdateInfo = new UpdateInfo();
                        mupdateInfo.setTestIP(testIP);
                        mupdateInfo.setRsaMd5(_rsaMd5);
                        mupdateInfo.setTagVersion(maskVersion);
                        mupdateInfo.setMd5(packInfo.getString("md5"));
                        mupdateInfo.setUrl(packInfo.getString("url"));;
                        updateUtils.setMaxVersionInfo(mupdateInfo);
                    }
                    if(compareVersion(version, maskVersion))
                    {
                        version = maskVersion;
                        System.out.println("parserJson update version "+maskVersion);
                        updateInfo.setTestIP(testIP);
                        updateInfo.setRsaMd5(_rsaMd5);
                        updateInfo.setTagVersion(maskVersion);
                        updateInfo.setMd5(packInfo.getString("md5"));
                        updateInfo.setUrl(packInfo.getString("url"));
                        JSONArray jMsg = packInfo.getJSONArray("msg");
                        int length = jMsg.length();
                        String[] msg = new String[jMsg.length()];

                        for (int i = 0; i < length; ++i) {
                            msg[i] = jMsg.getString(i);
                        }
                        updateInfo.setMessage(msg);
                        updateUtils.setUpdateInfo(updateInfo);
                        updateUtils.setMD5(updateInfo.getMd5());
                         ret = 0;
                    }
                }
            }
        } catch (Exception var15) {
            var15.printStackTrace();
        }

        return ret;
    }

    public static boolean isEmptyText(String text) {
        return text == null || text.length() == 0;
    }
}
